/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IteratedPrisonerDilemmaStrategyComponent.java
 *
 * Created on 26/Dez/2011, 22:44:25
 */

package intopo.game.impl.ipd;

import intopo.game.gui.AbstractStrategyEditorComponent;
import intopo.game.gui.ParsingException;
import java.awt.BorderLayout;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 26/Dez/2011
 */
public class IteratedPrisonerDilemmaStrategyComponent
	extends AbstractStrategyEditorComponent<IteratedPrisonerDilemmaStrategy>
{
	/**
	 * Bit mask that indicates if there is an error in any of the strategy parameters.
	 */
	private int error;
	/** Creates new form BeanForm */
	public IteratedPrisonerDilemmaStrategyComponent ()
	{
		initComponents();
		this.add (this.infoPanel, BorderLayout.SOUTH);
		this.updateInfoPanel (true);
		this.error = 0;
	}
	@Override
	public String getConfigFileDescription () {
		throw new UnsupportedOperationException ("Not supported yet.");
	}

	@Override
	public IteratedPrisonerDilemmaStrategy getStrategy ()
	{
		if (error == 0) {
			float cooperateFirstIteration = (Float) this.probabilityCooperateFirstIterationFormattedTextField.getValue ();
			float cooperateIfDefected = (Float) this.probabilityCooperatePartnerDefectedFormattedTextField.getValue ();
			float cooperateIfCooperated = (Float) this.probabilityCooperatePartnerCooperatedFormattedTextField.getValue ();
			return new IteratedPrisonerDilemmaProbabilisticStrategy (cooperateFirstIteration, cooperateIfDefected, cooperateIfCooperated);
		}
		else {
			return null;
		}
	}

	@Override
	public void setStrategy (IteratedPrisonerDilemmaStrategy strategy)
	{
		if (strategy instanceof IteratedPrisonerDilemmaProbabilisticStrategy) {
			IteratedPrisonerDilemmaProbabilisticStrategy s;
			s = (IteratedPrisonerDilemmaProbabilisticStrategy) strategy;
			this.probabilityCooperateFirstIterationFormattedTextField.setValue (new Float (s.cooperateFirstIteration ()));
			this.probabilityCooperatePartnerCooperatedFormattedTextField.setValue (new Float (s.cooperateIfCooperated ()));
			this.probabilityCooperatePartnerDefectedFormattedTextField.setValue (new Float (s.cooperateIfDefected ()));
		}
		else {
			throw new Error ("Unsupoorted strategy " + strategy.getClass ().getCanonicalName ());
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      javax.swing.JPanel parametersPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      probabilityCooperateFirstIterationFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      probabilityCooperatePartnerCooperatedFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
      probabilityCooperatePartnerDefectedFormattedTextField = new javax.swing.JFormattedTextField();

      setBorder(javax.swing.BorderFactory.createTitledBorder("strategy parameters"));

      parametersPanel.setLayout(new java.awt.GridBagLayout());

      jLabel1.setText("probability cooperate first turn: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      parametersPanel.add(jLabel1, gridBagConstraints);

      probabilityCooperateFirstIterationFormattedTextField.setColumns(5);
      probabilityCooperateFirstIterationFormattedTextField.setValue(new Float (1f));
      probabilityCooperateFirstIterationFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            probabilityCooperateFirstIterationFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(probabilityCooperateFirstIterationFormattedTextField, gridBagConstraints);

      jLabel2.setText("probability cooperate if partner cooperated: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      parametersPanel.add(jLabel2, gridBagConstraints);

      probabilityCooperatePartnerCooperatedFormattedTextField.setColumns(5);
      probabilityCooperatePartnerCooperatedFormattedTextField.setValue(new Float (1f));
      probabilityCooperatePartnerCooperatedFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            probabilityCooperatePartnerCooperatedFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(probabilityCooperatePartnerCooperatedFormattedTextField, gridBagConstraints);

      jLabel3.setText("probability cooperate first turn: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      parametersPanel.add(jLabel3, gridBagConstraints);

      probabilityCooperatePartnerDefectedFormattedTextField.setColumns(5);
      probabilityCooperatePartnerDefectedFormattedTextField.setValue(new Float (1f));
      probabilityCooperatePartnerDefectedFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            probabilityCooperatePartnerDefectedFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      parametersPanel.add(probabilityCooperatePartnerDefectedFormattedTextField, gridBagConstraints);

      add(parametersPanel);
   }// </editor-fold>//GEN-END:initComponents

	 private void probabilityCooperateFirstIterationFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_probabilityCooperateFirstIterationFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.probabilityCooperateFirstIterationFormattedTextField.getValue ();
			final boolean ok = (value >= 0) && (value <= 1);
			if (ok) {
				this.error &= ~1;
			}
			else {
				this.error |= 1;
			}
			if (ok) {
				this.updateInfoPanel (true);
			}
			this.update (this.probabilityCooperateFirstIterationFormattedTextField, ok);
		}
	 }//GEN-LAST:event_probabilityCooperateFirstIterationFormattedTextFieldPropertyChange

	 private void probabilityCooperatePartnerCooperatedFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_probabilityCooperatePartnerCooperatedFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.probabilityCooperatePartnerCooperatedFormattedTextField.getValue ();
			final boolean ok = (value >= 0) && (value <= 1);
			if (ok) {
				this.error &= ~1;
			}
			else {
				this.error |= 1;
			}
			if (ok) {
				this.updateInfoPanel (true);
			}
			this.update (this.probabilityCooperatePartnerCooperatedFormattedTextField, ok);
		}
}//GEN-LAST:event_probabilityCooperatePartnerCooperatedFormattedTextFieldPropertyChange

	 private void probabilityCooperatePartnerDefectedFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_probabilityCooperatePartnerDefectedFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.probabilityCooperatePartnerDefectedFormattedTextField.getValue ();
			final boolean ok = (value >= 0) && (value <= 1);
			if (ok) {
				this.error &= ~1;
			}
			else {
				this.error |= 1;
			}
			if (ok) {
				this.updateInfoPanel (true);
			}
			this.update (this.probabilityCooperatePartnerDefectedFormattedTextField, ok);
		}
}//GEN-LAST:event_probabilityCooperatePartnerDefectedFormattedTextFieldPropertyChange



   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField probabilityCooperateFirstIterationFormattedTextField;
   private javax.swing.JFormattedTextField probabilityCooperatePartnerCooperatedFormattedTextField;
   private javax.swing.JFormattedTextField probabilityCooperatePartnerDefectedFormattedTextField;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
